﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using Jage.Utils;

namespace Radar.Navigation
{
    internal class WaypointConnector
        : SingletonBase<WaypointConnector>
    {

        public void ConnectWaypoints(NavMesh navMesh)
        {
#if DEBUG
            connectionCount = 0;
#endif
            foreach (var zone in navMesh.Zones)
            {
                foreach (var link in zone.Links)
                {
                    ConnectWaypoint(link.CornerA, zone);
                    ConnectWaypoint(link.CornerB, zone);
                }
            }
#if DEBUG
            Debug.WriteLine("Waypoint connections: " + connectionCount);
#endif
        }

        private void ConnectWaypoint(Waypoint waypoint, NavZone zone)
        {
            foreach (Link link in zone.Links)
            {
                ConnectWaypoint(waypoint, link.CornerA);
                ConnectWaypoint(waypoint, link.CornerB);
            }
        }

#if DEBUG
        int connectionCount = 0;
#endif

        private void ConnectWaypoint(Waypoint current, Waypoint neighbour)
        {
            if (current != neighbour)
            {
                if (!current.Neighbours.Contains(neighbour))
                {
#if DEBUG
                    connectionCount++;
#endif
                    current.Neighbours.Add(neighbour);
                }
            }
        }

    }
}
